const express = require('express');
const router = express.Router();
const { body } = require('express-validator');
const whatsappController = require('../controllers/whatsappController');
const { authenticate, isAdmin } = require('../middlewares/authMiddleware');
const { validate } = require('../middlewares/validationMiddleware');

// 获取WhatsApp连接状态
router.get('/status', authenticate, whatsappController.getStatus);

// 获取二维码
router.get('/qrcode', authenticate, whatsappController.getQRCode);

// 初始化WhatsApp连接
router.post('/initialize', authenticate, whatsappController.initialize);

// 重新连接WhatsApp
router.post('/reconnect', authenticate, whatsappController.reconnect);

// 断开连接
router.post('/disconnect', authenticate, whatsappController.disconnect);

// 用户专属的WhatsApp连接管理路由
// 获取用户专属的连接状态
router.get('/user/:userId/status', authenticate, whatsappController.getUserStatus);

// 获取用户专属的二维码
router.get('/user/:userId/qrcode', authenticate, whatsappController.getUserQRCode);

// 初始化用户专属的WhatsApp连接
router.post('/user/:userId/initialize', authenticate, whatsappController.initializeUser);

// 断开用户专属的WhatsApp连接
router.post('/user/:userId/disconnect', authenticate, whatsappController.disconnectUser);

// 获取所有联系人
router.get('/contacts', authenticate, whatsappController.getContacts);

// 获取单个联系人详情
router.get('/contacts/:id', authenticate, whatsappController.getContact);

// 获取所有聊天
router.get('/chats', authenticate, whatsappController.getChats);

// 获取设备信息
router.get('/device-info', authenticate, whatsappController.getDeviceInfo);

// 设置状态信息
router.post(
  '/status',
  [
    authenticate,
    body('status').notEmpty().withMessage('状态信息是必须的'),
    validate
  ],
  whatsappController.setStatus
);

module.exports = router;